package cn.yitun.chat.yitunserverimapicore.business.personal.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.Info;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.action.MethodMapping;
import cn.yitun.chat.yitungeneral.annotation.codec.Encryped;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.user.ResetPwdDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.user.UserDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.userchat.UserHeadDto;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunservercommonmessage.data.user.LoginDto;
import cn.yitun.chat.yitunserverimapicore.business.personal.service.PersonalService;
import cn.yitun.chat.yitunserverimapicore.business.user.service.UserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Date 2019-01-20 08:38:14<br>
 * Description 个人信息业务处理
 * @author yitun<br>
 * @version 1.1
 */
@Component
@ActionMapping(value = ActionTypeConstant.PERSONAL_ACTION_PUSH)
public class PersonalAction {

	@Resource
	private UserService userService;
	@Resource
	private PersonalService personalService;

	/**
	 * Date 2019-01-20 08:39:39<br>
	 * Description 用户注册
	 * @param user ：用户信息
	 * user.getSecurityQuestionList()：密保问列表
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	@MethodMapping(value = "", isIntercept = false)
	@ActionMapping(value = "1.1.0001")
	public Object register(@JsonDefine("data") UserDto user) {

		if (YitunCollectionUtil.isEmpty(user.getSecurityQuestionList()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		ResultMessage message = new ResultMessage();
		try {
			Info info = personalService.register(user, user.getSecurityQuestionList());
			message.bodyPut("user", userService.getVO(user.getId()));
			message.setInfo(info);
		} catch (Exception e) {
			e.printStackTrace();
			message.addError(ExceptionEnum.BUSY_SERVER);
			message.addWarning(ExceptionEnum.REGISTER_FAIL);
		}
		return message;
	}

	/**
	 * Date 2019-01-20 08:42:42<br>
	 * Description 登录
	 * @param socketSession
	 * @param loginData
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	@MethodMapping(value = "", isIntercept = false)
	@ActionMapping(value = "1.1.0005")
	public ResultMessage login(SocketSession socketSession, @JsonDefine("data") LoginDto loginData) {

		return personalService.login(loginData);
	}

	/**
	 * Date 2019-01-20 08:48:32<br>
	 * Description 获取个人信息
	 * @param socketSession
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0007")
	public Object getUserData(SocketSession socketSession) {
		return new ResultMessage(R.param("user", userService.getUserDataById(socketSession.getKey())));
	}

	/**
	 * 获取用户头像(如果选取了系统头像)
	 * @param socketSession
	 * @return
	 */
	@ActionMapping(value = "1.1.0008")
	public Object getUserHead(SocketSession socketSession) {
		Long userId = socketSession.getKey();
		return new ResultMessage(R.param("head", personalService.getUserHeadByUserId(userId)));
	}

	/**
	 * Date 2019-01-20 08:48:56<br>
	 * Description 修改个人信息
	 * @param user
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0009")
	public ResultMessage updateUser(SocketSession socketSession, @JsonDefine("data") UserDto user) {
		if (NumUtil.isInValid(user.getId()) || !user.getId().equals(socketSession.getKey()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		ResultMessage message = new ResultMessage();
		message.setInfo(personalService.updatePersonal(user));
		return message;
	}

	/**
	 * Date 2019-01-20 08:50:08<br>
	 * Description 修改密码
	 * @param socketSession
	 * @param query
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	@Encryped(decode = true)
	@ActionMapping(value = "1.1.0010")
	public ResultMessage updatePassword(
			SocketSession socketSession,
			@JsonDefine("data") ResetPwdDto query) {
		if (YitunStringUtil.isEmpty(query.getNewPassword()) || YitunStringUtil.isEmpty(query.getOldPassword()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		return personalService.updatePassword(socketSession.getKey(), query.getOldPassword(), query.getNewPassword());
	}

	/**
	 * 
	 * Date 2019-01-20 08:51:01<br>
	 * Description 修改头像
	 * 
	 * @param userHead
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0011")
	public ResultMessage uploadHead(SocketSession socketSession, @JsonDefine("data") UserHeadDto userHead) {
		userHead.setUserId(socketSession.getKey());
		return personalService.uploadHead(userHead);
	}

	/**
	 * 
	 * Date 2019-01-20 08:52:19<br>
	 * Description 修改个性签名
	 * 
	 * @param socketSession
	 * @param user
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0012")
	public void updateSignature(SocketSession socketSession, @JsonDefine("data") UserDto user) {
		personalService.updateSignature(socketSession.getKey(), user.getSignature());

	}

	/**
	 * 
	 * Date 2019-01-20 10:01:00<br>
	 * Description 修改在线状态
	 * 
	 * @param socketSession
	 * @param user
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0013")
	public void updateStatus(SocketSession socketSession, @JsonDefine("data") UserDto user) {
		if (YitunStringUtil.isEmpty(user.getChatOnline()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		personalService.updateChatOnlineStatus(socketSession.getKey(), user.getChatOnline());
	}
}
